import android.text.TextUtils
import com.compass.mobile.framework.constant.ALL_DOCTOR
import com.compass.mobile.models.Dictionaries
import com.compass.mobile.framework.constant.HOSPITAL_CODE
import com.compass.mobile.framework.constant.HOSPITAL_ID
import com.compass.mobile.framework.constant.INSPECTION_REPORT
import com.compass.mobile.framework.constant.MESSAGE_TYPE
import com.compass.mobile.framework.constant.ORDER_CONSULTATION_TYPE
import com.compass.mobile.framework.constant.ORDER_INSPECTION_TYPE
import com.compass.mobile.framework.constant.ORDER_NURSE_STATUS
import com.compass.mobile.framework.constant.ORDER_TYPE
import com.compass.mobile.framework.constant.PLATFORM_ID
import com.compass.mobile.framework.constant.USER_ACCESS_TOKEN
import com.compass.mobile.framework.constant.USER_ACC_ID
import com.compass.mobile.framework.constant.USER_IM_TOKEN
import com.compass.mobile.framework.constant.USER_INFO_DATA
import com.compass.mobile.framework.constant.USER_REFRESH_TOKEN

import com.compass.mobile.models.PatientBean
import com.tencent.mmkv.MMKV


/**
 * Date:2023/11/16 17:04
 * @desc   用户信息管理类
 */
object UserInfoManager {

    private var mmkv = MMKV.defaultMMKV()


    fun putString(key: String, value: String?) {
        mmkv.encode(key, value)
    }

    fun getString(key: String): String? {
        return mmkv.decodeString(key, "")
    }

    fun isLogin(): Boolean {
        return !TextUtils.isEmpty(getImUserAccount())
    }

    /**
     *  问诊单类型
     */
    fun saveOrderType(status: String?) {
        mmkv.encode(ORDER_CONSULTATION_TYPE, status)
    }

     fun getOrderTypeSource(): String? {
        return mmkv.decodeString(ORDER_CONSULTATION_TYPE, "")
    }
    fun getOrderTypeString(key:String):String{
        val list = getOrderTypeSource()?.toBeanOrNull<MutableList<Dictionaries>>()
        list?.let {
            for (item in list){
                if (item.value == key) return item.label
            }
        }
        return ""
    }





    /**
     *  通知类型
     */
    fun saveNotificationType(status: String?) {
        mmkv.encode(MESSAGE_TYPE, status)
    }

    private fun getNotificationTypeSource(): String? {
        return mmkv.decodeString(MESSAGE_TYPE, "")
    }

    fun getNotificationTypeString(key:String):String{
        val list = getNotificationTypeSource()?.toBeanOrNull<MutableList<Dictionaries>>()
        list?.let {
            for (item in list){
                if (item.value == key) return item.label
            }
        }
        return ""
    }


    /**
     *  检验检查
     */
    fun saveInspectionType(status: String?) {
        mmkv.encode(ORDER_INSPECTION_TYPE, status)
    }

    private fun getInspectionTypeSource(): String? {
        return mmkv.decodeString(ORDER_INSPECTION_TYPE, "")
    }

    fun getInspectionTypeString(key:String):String{
        val list = getInspectionTypeSource()?.toBeanOrNull<MutableList<Dictionaries>>()
        list?.let {
            for (item in list){
                if (item.value == key) return item.label
            }
        }
        return ""
    }

    /**
     *  保存报告单
     */
    fun saveInspectionReportType(usage: String?) {
        mmkv.encode(INSPECTION_REPORT, usage)
    }

    /**
     * 获取报告单
     */
    fun getInspectionReportType(): String? {
        return mmkv.decodeString(INSPECTION_REPORT, "")
    }


    /**
     *  保存报告单
     */
    fun saveOrderNurseStatusType(usage: String?) {
        mmkv.encode(ORDER_NURSE_STATUS, usage)
    }

    /**
     * 获取报告单
     */
    fun getOrderNurseStatusType(): String? {
        return mmkv.decodeString(ORDER_NURSE_STATUS, "")
    }


    /**
     *  保存报告单
     */
    fun saveAllDoctorType(usage: String?) {
        mmkv.encode(ALL_DOCTOR, usage)
    }

    /**
     * 获取报告单
     */
    fun getAllDoctor(): String? {
        return mmkv.decodeString(ALL_DOCTOR, "")
    }


    /**
     *  问诊类型
     */
    fun saveOrderConsultationType(status: String?) {
        mmkv.encode(ORDER_TYPE, status)
    }

    private fun getOrderConsultationType(): String? {
        return mmkv.decodeString(ORDER_TYPE, "")
    }

    fun getOrderConsultationTypeString(key:String):String{
        val list = getOrderConsultationType()?.toBeanOrNull<MutableList<Dictionaries>>()
        list?.let {
            for (item in list){
                if (item.value == key) return item.label
            }
        }
        return ""
    }


    /**
     * 是否有默认患者
     */
    fun isHaveDefPatient(): Boolean {
        getPatientInfo()?.let {
            return true
        } ?: kotlin.run {
            return false
        }

    }


    /**
     * 保存默认患者
     */
    fun savePatientInfo(patientString: String?) {
        mmkv.encode(USER_INFO_DATA, patientString)
    }


    fun saveRefreshToken(token: String?) {
        mmkv.encode(USER_REFRESH_TOKEN, token)
    }

    fun getRefreshToken(): String? {
        return mmkv.decodeString(USER_REFRESH_TOKEN, "")
    }

    fun saveAccessToken(accessToken: String?) {
        mmkv.encode(USER_ACCESS_TOKEN, accessToken)
    }

    fun saveImUserAccount(accId: String?) {
        mmkv.encode(USER_ACC_ID, accId)
    }

    fun getImUserAccount(): String? {
        return mmkv.decodeString(USER_ACC_ID, "")
    }

    fun saveImToken(imToken: String?) {
        mmkv.encode(USER_IM_TOKEN, imToken)
    }

    fun getImUserToken(): String? {
        return mmkv.decodeString(USER_IM_TOKEN, "")
    }

    fun getAccessToken(): String? {
        return mmkv.decodeString(USER_ACCESS_TOKEN)
    }


    fun getPatientInfo(): PatientBean? {
        val info = mmkv.decodeString(USER_INFO_DATA)
        return info?.toBeanOrNull<PatientBean>()
    }

    fun saveHospitalId(token: String?) {
        mmkv.encode(HOSPITAL_ID, token)
    }

    fun getHospitalId(): String? {
        return mmkv.decodeString(HOSPITAL_ID, "")
    }

    fun saveHospitalCode(token: String?) {
        mmkv.encode(HOSPITAL_CODE, token)
    }

    fun getHospitalIdCode(): String? {
        return mmkv.decodeString(HOSPITAL_CODE, "")
    }

    fun savePlatformId(platformId: String?) {
        mmkv.encode(PLATFORM_ID, platformId)
    }

    fun getPlatformId(): String? {
        return mmkv.decodeString(PLATFORM_ID, "")
    }

    /**
     * 清除用户信息、token、imAccId
     */
    fun clearUserInfo() {
        mmkv.encode(USER_INFO_DATA, "")
        saveRefreshToken("")
        saveAccessToken("")
        saveImUserAccount("")
        saveImToken("")


    }


}